﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class PDKSettlement : MonoBehaviour {

    EginUser user = EginUser.Instance;
    EginPDK pdk = EginPDK.Instance;
    public List<GameObject> PDKSettlementObj;
    public List<UISprite> avatarList;
    public List<UITexture> wxAvatarList;
    public List<UILabel> usernameList;
    public List<UILabel> uidList;

    //scroe
    public List<GameObject> Score;
    public List<GameObject> ScoreFather;
    public List<UISprite> jiajian;
    //rank
    public List<UISprite> rank1;
    public List<UISprite> rankother;

    //bg
    public List<UISprite> ownBg;
    public GameObject BackHall;
    void Awake()
    {

        UIEventListener.Get(BackHall).onClick = ButtonClick;
    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "BackHall")
        {
            EginUser.Instance.gameUserInfoList.Clear();
            //if (user.gameType == 1)
            //{
            //    Application.LoadLevel(ScenePath.GameMahjong);
            //}
            //else if (user.gameType == 2)
            //{
            //    Application.LoadLevel(ScenePath.GameDN);
            //}
            //else if (user.gameType == 3)
            //{
            //    Application.LoadLevel(ScenePath.GameDDZ);
            //}
            //else if (user.gameType == 4)
            //{
            //    //Application.LoadLevel(ScenePath.GameZJH);
            //}

            Application.LoadLevel(ScenePath.Hall);
        }
    }

    public void LoadInfo(List<GameOverUserInfo> gameOverUserInfo)
    {
        int tempIndex = 0;
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 1)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 2)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 3)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        //for (int i = 0; i < gameOverUserInfo.Count; i++)
        //{
        //    if (gameOverUserInfo[i].TotalRank == 4)
        //    {
        //        LoadUserInfo(gameOverUserInfo, tempIndex, i);
        //        tempIndex++;
        //    }
        //}
        //for (int i = 0; i < gameOverUserInfo.Count; i++)
        //{
        //    if (gameOverUserInfo[i].TotalRank == 5)
        //    {
        //        LoadUserInfo(gameOverUserInfo, tempIndex, i);
        //        tempIndex++;
        //    }
        //}
    }

    void LoadUserInfo(List<GameOverUserInfo> gameOverUserInfo, int tempIndex, int i)
    {

        #region
        PDKSettlementObj[tempIndex].SetActive(true);
        int tempSeatID = gameOverUserInfo[i].SeatId;            
        for (int j = 0; j < user.gameUserInfoList.Count; j++)
        {            
            if (tempSeatID == user.gameUserInfoList[j].SeatId)
            {
                Debug.Log("user.gameUserInfoList[j].AvatarUrl = " + user.gameUserInfoList[j].AvatarUrl);
                if (user.gameUserInfoList[j].AvatarUrl != "")
                {
                    WXAvatarManager._instance.GetWxAvatar(user.gameUserInfoList[j].Uid, wxAvatarList[tempIndex], user.gameUserInfoList[j].AvatarUrl);
                    wxAvatarList[tempIndex].gameObject.SetActive(true);
                }
                else
                {
                    avatarList[tempIndex].spriteName = user.gameUserInfoList[j].AvatarId.ToString();
                }                
                usernameList[tempIndex].text = user.gameUserInfoList[j].Nickname;
                uidList[tempIndex].text = user.gameUserInfoList[j].Uid.ToString();
            }
        }
        

        int tempScore = gameOverUserInfo[i].TotalScore;

        string tempStr = user.ReturnMoney(Mathf.Abs(gameOverUserInfo[i].TotalScore));
        Debug.Log(tempStr);
        float x = Score[tempIndex].transform.localPosition.x;
        float y = Score[tempIndex].transform.localPosition.y;
        if (tempScore >= 0)
        {
            jiajian[tempIndex].gameObject.SetActive(true);
            jiajian[tempIndex].spriteName = "plus_" + "jia";
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score[tempIndex]);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather[tempIndex].transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 30;

                    }
                    else if (str == "w")
                    {
                        x += 50;
                    }
                    else
                    {
                        x += 40;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "plus_" + str;
            }
        }
        else
        {
            jiajian[tempIndex].gameObject.SetActive(true);
            jiajian[tempIndex].spriteName = "less_" + "jian";
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score[tempIndex]);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather[tempIndex].transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 30;
                    }
                    else if (str == "w")
                    {
                        x += 50;
                    }
                    else
                    {
                        x += 40;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "less_" + str;
            }
        }

        #endregion
        Debug.Log("gameOverUserInfo[i].TotalRank = " + gameOverUserInfo[i].TotalRank);
        int rank = gameOverUserInfo[i].TotalRank;
        if (rank == 1)
        {
            rank1[tempIndex].gameObject.SetActive(true);
        }
        else
        {
            rankother[tempIndex].gameObject.SetActive(true);
            rankother[tempIndex].spriteName = rank.ToString();
        }
        int sid = pdk.seatId;
        //if (user.gameType == 1)
        //{
        //    sid = EginMahjong.Instance.seatID;
        //}
        //else if (user.gameType == 2)
        //{
        //    sid = EginDN.Instance.seatId;
        //}
        //else if (user.gameType == 3)
        //{
        //    sid = EginDDZ.Instance.seatId;
        //}
        //else if (user.gameType == 4)
        //{
        //    //sid = EginZJH.Instance.seatId;
        //}

        if (gameOverUserInfo[i].SeatId == sid)
        {
            ownBg[tempIndex].spriteName = "zijibg";
        }
    }
}
